package com.zlsy.learning.leetcode.queue;

/**
 * @author zhouliang
 * @date 2020/8/13 15:06
 */
public class LinkedQueue {

  /** * 记录队列元素个数 */
  private int size = 0;
  /** head指向队头结点 */
  private Node head;
  /** tail指向队尾节点 */
  private Node tail;

  /** 申请一个队列 */
  public LinkedQueue() {}

  /**
   * 入队
   *
   * @param item
   * @return
   */
  public boolean enqueue(String item) {
    Node newNode = new Node();
    newNode.value = item;
    if (size == 0) {
      head = newNode;
    } else {
      tail.next = newNode;
    }
    tail = newNode;
    size++;
    return true;
  }

  /**
   * 出队
   *
   * @return
   */
  public String dequeue() {
    String res;
    if (size == 0) {
      return null;
    }
    if (size == 1) {
      tail = null;
    }
    res = head.value;
    head = head.next;
    size--;
    return res;
  }
}

class Node {
  String value;
  Node next;
}
